package service

import (
	"EnjoyWorking/model"
	"EnjoyWorking/utils"
	"EnjoyWorking/utils/storage"
	"errors"
	"time"
)

type Wpr struct {}


func (service *Wpr) FindOne(cond *model.Wpr) (*model.Wpr, error) {
	var (
		err   error
		resut = model.Wpr{}
	)

	if err = storage.MySqlDb().Where(cond).First(&resut).Error; err != nil {
		return nil, err
	}

	if err = storage.MySqlDb().Model(&resut).Find(&resut).Error; err != nil {
		return nil, err
	}

	return &resut, nil
}

func (service *Wpr) isExist(id uint) (bool, error) {
	var (
		err   error
		total int64
	)

	//检测工作计划总结表是否存在
	if err = storage.MySqlDb().Model(&model.Wpr{}).Where("id = ?", id).Count(&total).Error; err != nil {
		goto ERR
	}

	if total == 0 {
		return false, nil
	}

	return true, nil
ERR:
	return false, err
}

func (service *Wpr) Insert(item *model.Wpr) error {
	//检验用户是否存在
	var total int64
	if err := storage.MySqlDb().Model(&model.User{}).Where("id = ?", item.MakerID).Count(&total).Error; err != nil {
		return err
	}

	if total == 0 {
		return errors.New(utils.CODE_USER_NOT_EXIST.String())
	}

	//检验同一个用户是否存在相同月份的工作计划总结
	if err := storage.MySqlDb().
		Model(item).
		Where("maker_id = ? and create_date = ?", item.MakerID, item.CreateDate).
		Count(&total).Error; err != nil  {
		return err
	}

	if total != 0 {
		return errors.New(utils.CODE_WPR_CREATEDATE_DUPLICATE.String())
	}

	return storage.MySqlDb().Create(item).Error
}

func (service *Wpr) FindByUserId(userId uint) (int64, []*model.Wpr, error) {
	var (
		err    error
		total  int64
		result = make([]*model.Wpr, 0)
	)

	if err = storage.MySqlDb().Model(&model.Wpr{}).
		Where("maker_id = ?", userId).
		Count(&total).Error; err != nil {
		goto ERR
	}

	if err = storage.MySqlDb().Model(&model.Wpr{}).
		Where("maker_id = ?", userId).
		Preload("CheckUser").
		Preload("MakerUser").
		Find(&result).Error; err != nil {
		goto ERR
	}

	return total, result, nil
ERR:
	return 0, nil, err
}

func (service *Wpr) Delete(ids []int64, makerID uint) error {
	var (
		err   error
		wpr *model.Wpr
		isExist bool

		tx = storage.MySqlDb().Begin()
	)

	for _, id := range ids {
		if isExist, err = service.isExist(uint(id)); err != nil {
			goto ERR
		}

		if !isExist {
			return errors.New(utils.CODE_WPR_NOT_EXIST.String())
		}

		if wpr, err = service.FindOne(&model.Wpr{ID:uint(id)}); err != nil {
			goto ERR
		}

		if wpr.MakerID != makerID {
			return errors.New(utils.CODE_WPR_DELETE_ERR.String())
		}

		//删除工作计划总结表项
		if err = tx.Delete(&model.WprItem{}).Where("parent_id in (?)", wpr.ID).Error; err != nil {
			return err
		}

		//删除工作计划总结
		if err = tx.Delete(&model.Wpr{}).Where("id in (?)", id).Error; err != nil {
			return err
		}
	}
	tx.Commit()

ERR:
	tx.Rollback()
	return err
}


func (service *Wpr) Check(id, checkerID uint, comments string) error {
	var (
		err   error
		total int64
		wpr *model.Wpr
		isExist bool
	)

	if isExist, err = service.isExist(id); err != nil {
		goto ERR
	}

	if !isExist {
		return errors.New(utils.CODE_WPR_NOT_EXIST.String())
	}

	if wpr, err = service.FindOne(&model.Wpr{ID:id}); err != nil {
		goto ERR
	}

	//检测审核者是否存在
	if err = storage.MySqlDb().Model(&model.User{}).Where("id = ?", checkerID).Count(&total).Error; err != nil {
		goto ERR
	}

	if total == 0 {
		return errors.New(utils.CODE_USER_NOT_EXIST.String())
	}

	if wpr.MakerID == checkerID {
		return errors.New(utils.CODE_WPR_CHECKER_ERR.String())
	}

	if err = storage.MySqlDb().
		Model(&model.Wpr{ID: id}).
		Updates(model.Wpr{CheckerID:checkerID, CheckTime:time.Now(), Comments:comments}).Error; err != nil {
		goto ERR
	}

ERR:
	return err
}